<?php
namespace Modules\Menu\Controllers;

use Core\Mvc\Controller;

class AdminController extends Controller
{
    public function indexAction($menu)
    {
        $menuEntity = $this->entityManager->get('configList', [
            'contentModel' => 'menu',
            'id' => $menu
        ]);
        if (!$menuEntity) {
            $this->flashSession->error('菜单不存在：' . $menu);
            return $this->moved(array(
                'for' => 'adminMenuLinkList',
                'menu' => $menu,
            ));
        }

        $menuData = $this->config->get('m.menu.menu' . ucfirst($menu) . 'Data', array());
        $menuHierarchy = $this->config->get('m.menu.menu' . ucfirst($menu) . 'Hierarchy', array());

        if ($this->request->isPost() && $this->request->hasPost('rh')) {
            $rh = json_decode($this->request->getPost('rh'));
            $rh = nestableJsonToArray($rh);
            $state = $this->config->set('m.menu.menu' . ucfirst($menu) . 'Hierarchy', $rh);
            if ($state) {
                $menuHierarchy = $rh;
                $this->flashSession->success('菜单排序成功');
            } else {
                $this->flashSession->error('菜单排序失败');
            }
        }
        self::$variables += array(
            '#templates' => 'pageNoWrapper',
            'menuId' => $menu,
            'content' => array(),
        );
        $content['menuList'] = array(
            '#templates' => array(
                'box',
            ),
            'wrapper' => false,
            'id' => 'right_handle',
            'title' => $menuEntity->name . ' 链接排序',
            'max' => true,
            'color' => 'success',
            'size' => '6',
            'content' => array(
                'menuLinkSort' => array(
                    '#templates' => array(
                        'hierarchy',
                        'hierarchy-adminMenuLink',
                    ),
                    'id' => 'menuLinkHierarchy',
                    'title_display' => false,
                    'data' => $menuData,
                    'hierarchy' => $menuHierarchy,
                ),
            ),
        );

        self::$variables['content'] += $content;
    }

    public function linkAddAction($menu)
    {
        $content = array();
        $menuEntity = $this->entityManager->get('configList', [
            'contentModel' => 'menu',
            'id' => $menu
        ]);

        if (!$menuEntity) {
            $this->flashSession->error('菜单不存在：' . $menu);
            return $this->moved(array(
                'for' => 'adminMenuLinkList',
                'menu' => $menu,
            ));
        }
        $menuFormInfo = $this->config->get('menu.menuLinkForm');
        $menuFormInfo['menu']['default'] = $menu;
        $menuLinkAddForm = $this->form->create($menuFormInfo);
        if ($menuLinkAddForm->isValid()) {
            $menuLinkAddForm->save();
            return $this->moved(array(
                'for' => 'adminMenuLinkList',
                'menu' => $menu,
            ));
        }
        self::$variables += array(
            '#templates' => 'pageNoWrapper',
            'content' => array(),
        );
        $content['menuList'] = array(
            '#templates' => 'box',
            'wrapper' => false,
            'id' => 'right_handle',
            'title' => $menuEntity->name . ' 链接添加',
            'max' => true,
            'color' => 'success',
            'size' => '6',
            'content' => array(
                'menuLinkAdd' => $menuLinkAddForm->renderForm(),
            ),
        );
        self::$variables['content'] += $content;
    }

    public function linkEditorAction($menu, $link)
    {
        $menuEntity = $this->entityManager->get('configList', [
            'contentModel' => 'menu',
            'id' => $menu
        ]);

        if (!$menuEntity) {
            $this->flashSession->error('菜单不存在：' . $menu);
            return $this->moved(array(
                'for' => 'adminMenuLinkList',
                'menu' => $menu,
            ));
        }

        $menuData = $this->config->get('m.menu.menu' . ucfirst($menu) . 'Data', array());
        if (!isset($menuData[$link])) {
            $this->flashSession->error('链接不存在');
            return $this->moved(array(
                'for' => 'adminMenuLinkList',
                'menu' => $menu,
            ));
        }

        $content = [];
        $menuLinkAddForm = $this->form->create('menu.menuLinkForm', $menuData[$link]);

        if ($menuLinkAddForm->isValid()) {
            $menuLinkAddForm->save();
            return $this->moved(array(
                'for' => 'adminMenuLinkList',
                'menu' => $menu,
            ));
        }
        self::$variables += array(
            '#templates' => 'pageNoWrapper',
            'content' => array(),
        );
        $content['menuList'] = array(
            '#templates' => 'box',
            'wrapper' => false,
            'id' => 'right_handle',
            'title' => $menuEntity->name . ' 链接编辑',
            'max' => true,
            'color' => 'success',
            'size' => '12',
            'content' => array(
                'menuList' => $menuLinkAddForm->renderForm(),
            ),
        );
        self::$variables['content'] += $content;
    }

    public function linkDeleteAction($menu, $link)
    {
        $menuEntity = $this->entityManager->get('configList', [
            'contentModel' => 'menu',
            'id' => $menu
        ]);

        if (!$menuEntity) {
            $this->flashSession->error('菜单不存在：' . $menu);
            return $this->moved(array(
                'for' => 'adminMenuLinkList',
                'menu' => $menu,
            ));
        }

        if (!$menuEntity) {
            $this->flashSession->error('所要删除的菜单不存在');
        } else {
            $menuData = $this->config->get('m.menu.menu' . ucfirst($menu) . 'Data');
            $menuHierarchy = $this->config->get('m.menu.menu' . ucfirst($menu) . 'Hierarchy');
            if (isset($menuData[$link])) {
                unset($menuData[$link]);
            }
            $newMenuHierarchy = MultArrayDeleteItem($menuHierarchy, $link);
            if ($this->config->set('m.menu.menu' . ucfirst($menu) . 'Data', $menuData) && $this->config->set('m.menu.menu' . ucfirst($menu) . 'Hierarchy', $newMenuHierarchy)) {
                $this->flashSession->success('删除成功。');
            } else {
                $this->flashSession->error('删除失败。');
            }
        }
        return $this->moved(array(
            'for' => 'adminMenuLinkList',
            'menu' => $menu,
        ));
    }
}
